home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / admin / linuxcon.000 / linuxcon / linuxconf-1.6 / netconf / net.c < prev    next >
C/C++ Source or Header  |  1996-06-11  |  2KB  |  106 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <errno.h>
  4. #include <string.h>
  5. #include <stdarg.h>
  6. #include <ctype.h>
  7. #include "netconf.h"
  8. #include "../xconf/xconf.h"
  9. #include "netconf.m"
  10.  
  11. /*
  12.     Return != 0 if str is a valid ip number (lexically)
  13. */
  14. int net_isipnum(const char *str)
  15. {
  16.     int ret = 0;
  17.     int nbpt = 0;    // Number of period met
  18.     while (1){
  19.         if (!isdigit(*str)){
  20.             break;
  21.         }else{
  22.             int num = atoi(str);
  23.             while (isdigit(*str)) str++;
  24.             if (num > 255){
  25.                 break;
  26.             }else if (*str == '\0'){
  27.                 ret = 1;
  28.                 break;
  29.             }else if (*str != '.'){
  30.                 break;
  31.             }else{
  32.                 nbpt++;
  33.                 str++;
  34.             }
  35.         }
  36.     }
  37.     if (ret && nbpt != 3) ret = 0;
  38.     return ret;
  39. }
  40.  
  41. /*
  42.     Add a message in /usr/adm/netconf.log
  43. */
  44. void net_prtlog (const char *ctl, ...)
  45. {
  46.     char buf[10000];
  47.     va_list list;
  48.     va_start (list,ctl);
  49.     vsprintf (buf,ctl,list);
  50.     va_end (list);
  51.     if (simul_ison()){
  52.         simul_addmsg (buf);
  53.     }else{
  54.         FILE *fout = fopen (TMP_NETCONF_LOG,"a");
  55.         static char shown_err = 0;
  56.         /* #Specification: /usr/adm/netconf.log / can't open
  57.             Action taken by netconf are log in /usr/adm/netconf.log.
  58.             They can't be log with syslogd because syslogd is started
  59.             by netconf and may not be available.
  60.             
  61.             If the file can't be open, one and only one error message will
  62.             be shown (per netconf session).
  63.         */
  64.         if (fout != NULL){
  65.             fputs (buf,fout);
  66.             fclose (fout);
  67.         }else if (!shown_err){
  68.             shown_err = 1;
  69.             xconf_error (MSG_U(E_CANTOPEN,"Can't open file %s\n(%s)\n")
  70.                 ,TMP_NETCONF_LOG,strerror(errno));
  71.         }
  72.     }
  73. }
  74.  
  75. /*
  76.     Send a title string with a date in /var/adm/netconf.log
  77. */
  78. void net_introlog (const char *msg)
  79. {
  80.     char cmd[100];
  81.     sprintf (cmd,"echo %s:  `date`>>%s",msg,TMP_NETCONF_LOG);
  82.     system  (cmd);
  83. }
  84.  
  85. #ifdef TEST
  86.  
  87. static void test (const char *str)
  88. {
  89.     printf ("testing %s -> %s\n",str
  90.         ,net_isipnum(str) ? "OK" : "Not valid");
  91. }
  92.  
  93. int main(int argc, char *argv[])
  94. {
  95.     test ("hello");
  96.     test ("1.2.3");
  97.     test ("1.a.2.3");
  98.     test ("1a.2.3.4");
  99.     test ("11.22.33.444");
  100.     printf ("All other are valid\n");
  101.     test ("1.2.3.4");
  102.     test ("11.22.33.44");
  103.     return 0;
  104. }
  105.  
  106. #endif